<?php
/**
 * 使文件作为附件被下载
 */
$file = 'test.txt';

if (file_exists($file)) {
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($file).'"');

    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    readfile($file);//将文件读取到输出缓存中，相当于echo file_get_contents($file);???
    
    exit;
}
?>
